package com.example.xmpic.ui.activity.setting;

import android.content.Intent;
import android.view.View;

import com.example.common.base.OnCallBackToRefresh;
import com.example.common.utils.LiveDataBus;
import com.example.xmpic.R;
import com.example.xmpic.databinding.ActivityCameraSettingBinding;
import com.example.xmpic.support.FunSupport;
import com.example.xmpic.support.models.FunDevice;
import com.example.common.base.BasePresentActivity;
import com.example.xmpic.mvp.contract.CameraIncreaseContract;
import com.example.xmpic.mvp.present.CameraIncreasePresenter;
import com.example.xmpic.ui.data.CameraBean;
import com.example.xmpic.ui.utils.CameraDaoUtil;
import com.example.xmpic.view.BaseTipsDialog;

/**
 * @author skygge
 * @Date on 2020-02-19.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：设置界面
 */
public class CameraSettingMainActivity extends BasePresentActivity<CameraIncreasePresenter, ActivityCameraSettingBinding> implements View.OnClickListener, CameraIncreaseContract.IView {

    private int id;
    private String mDeviceSn;
    private CameraBean mCameraBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_camera_setting;
    }

    @Override
    protected void initialize() {
        super.initialize();
        id = getIntent().getIntExtra("deviceId", 0);
        mDeviceSn = getIntent().getStringExtra("deviceSn");
        FunDevice mFunDevice = FunSupport.getInstance().findDeviceById(id);
        if ( null == mFunDevice) {
            finish();
        }
    }

    @Override
    protected void initPresent() {
        super.initPresent();
        mPresent = new CameraIncreasePresenter(mContext);
    }

    @Override
    protected void initData() {
        super.initData();
        mCameraBean = CameraDaoUtil.getInstance().findCameraByCameraId(mDeviceSn);
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivSettingBack.setOnClickListener(this);
        mDBind.codeSetting.setOnClickListener(this);
        mDBind.common.setOnClickListener(this);
        mDBind.expertSetting.setOnClickListener(this);
        mDBind.storage.setOnClickListener(this);
        mDBind.tongYong.setOnClickListener(this);
        mDBind.alarmSetting.setOnClickListener(this);
        mDBind.deleteCamera.setOnClickListener(view -> showDeleteDialog());
    }

    private void showDeleteDialog(){
        BaseTipsDialog mBaseDialog = new BaseTipsDialog(mContext, new OnCallBackToRefresh() {
            @Override
            public void onConfirm() {
                showProgressDialog();
                mPresent.deleteCamera(mCameraBean.getUserId());
            }

            @Override
            public void onCancel() {

            }
        });
        mBaseDialog.setTitleAndButton(getString(R.string.confirm_delete_monitor), getString(R.string.cancel), getString(R.string.ok));
        mBaseDialog.show();
    }

    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.iv_setting_back) {
            finish();
        } else if (viewId == R.id.code_setting) {
            Intent h = new Intent(this, CameraSetPwdActivity.class);
            h.putExtra("FUN_DEVICE_ID", id);
            startActivity(h);
        } else if (viewId == R.id.common) {
            Intent h2 = new Intent(this, CameraSetCommonActivity.class);
            h2.putExtra("FUN_DEVICE_ID", id);
            h2.putExtra("FUN_DEVICE_SN", mDeviceSn);
            startActivity(h2);
        } else if (viewId == R.id.storage) {
            Intent h3 = new Intent(this, CameraSetStorageActivity.class);
            h3.putExtra("FUN_DEVICE_ID", id);
            startActivity(h3);
        } else if (viewId == R.id.expert_setting) {
            Intent j = new Intent(this, CameraSetExpertActivity.class);
            j.putExtra("FUN_DEVICE_ID", id);
            startActivity(j);
        } else if (viewId == R.id.tong_yong) {
            Intent h4 = new Intent(this, CameraSetInfoActivity.class);
            h4.putExtra("FUN_DEVICE_ID", id);
            startActivity(h4);
        } else if (viewId == R.id.alarm_setting) {
            Intent h5 = new Intent(this, CameraSetAlarmActivity.class);
            h5.putExtra("FUN_DEVICE_ID", id);
            h5.putExtra("FUN_DEVICE_SN", mDeviceSn);
            startActivity(h5);
        }
    }

    @Override
    public void hasExits() {

    }

    @Override
    public void showToastMsg(String msg) {
        dismissProgressDialog();
        showToast(getString(R.string.failed));
    }

    @Override
    public void bindSuccess() {

    }

    @Override
    public void deleteSuccess() {
        dismissProgressDialog();
        showToast(getString(R.string.delete_success));
        CameraDaoUtil.getInstance().deleteCamera(mCameraBean);
        LiveDataBus.get().with("refresh_camera").setValue("delete");
        finish();
    }
}
